package cn.tedu.ivoscm.distribute.service.impl;

import cn.tedu.ivoscm.application.mapper.ApplicationMapper;
import cn.tedu.ivoscm.application.pojo.dto.ApplicationQuery;
import cn.tedu.ivoscm.application.pojo.entity.Application;
import cn.tedu.ivoscm.application.pojo.vo.ApplicationVO;
import cn.tedu.ivoscm.base.enums.ApplicationStatusEnum;
import cn.tedu.ivoscm.distribute.pojo.dto.DistributeParam;
import cn.tedu.ivoscm.distribute.pojo.dto.DistributeQuery;
import cn.tedu.ivoscm.distribute.service.DistributeService;
import cn.tedu.ivoscm.vehicle.mapper.VehicleMapper;
import cn.tedu.ivoscm.vehicle.pojo.entity.Vehicle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class DistributeServiceImpl implements DistributeService {
    @Autowired
    private ApplicationMapper applicationMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<ApplicationVO> select(DistributeQuery dq) {
        log.debug("查询工单列表业务：{}", dq);
        ApplicationQuery aq = new ApplicationQuery();
        aq.setUsername(dq.getUsername());
        aq.setStatus(dq.getType());
        List<ApplicationVO> list = applicationMapper.select(aq);
        return list;
    }

    @Override
    public void allocation(DistributeParam dp) {
        log.debug("分配用车业务：{}", dp);
        // 更新工单状态
        Application a = new Application();
        a.setId(dp.getApplicationId());
        a.setStatus(ApplicationStatusEnum.ALLOCATION.getCode());
        a.setVehicleId(dp.getVehicleId());
        System.out.println(dp.getVehicleId());
        a.setUpdateTime(new Date());
        applicationMapper.update(a);
        // 更新车辆状态
        Vehicle v = new Vehicle();
        v.setId(dp.getVehicleId());
        v.setStatus("2");
        vehicleMapper.update(v);
    }

    @Override
    public void returnVehicle(DistributeParam dp) {
        log.debug("还车业务：{}", dp);
        // 更新工单状态
        Application a = new Application();
        a.setId(dp.getApplicationId());
        a.setStatus(ApplicationStatusEnum.END.getCode());
        a.setUpdateTime(new Date());
        applicationMapper.update(a);
        // 更新车辆状态
        Vehicle v = new Vehicle();
        v.setId(dp.getVehicleId());
        v.setStatus("1");
        vehicleMapper.update(v);
    }
}
